Explain the concept of prototypal inheritance in JavaScript.
Explain the concept of prototypal inheritance in JavaScript.
11826-Jun-2024
Updated on 26-Jun-2024
Home / DeveloperSection / Forums / Explain the concept of prototypal inheritance in JavaScript.
Explain the concept of prototypal inheritance in JavaScript.
Ashutosh Kumar Verma
26-Jun-2024JavaScript prototypal inheritance
Prototype inheritance is a key concept in JavaScript where objects inherit properties and methods from other objects rather than from classes as in traditional class-based inheritance models this is achieved through instances.
Key Concepts
Prototypes
null
).Prototype Chain
Constructor Functions
new
keyword, the newly created object inherits properties and methods from the prototype of the constructor function.prototype
Propertyprototype
property, where properties and methods can be added sequentially by instantiating them as constructors to that function.prototype
property.Also, Read: Prototypal Inheritance vs. Class-Based Inheritance in JavaScript
Example-
In the example above-
Person
is a constructor function that creates instances withname
andage
properties.The
sayHello
method has been added toPerson.prototype
, making it available to all instances created byPerson
.person1
andperson2
inherit properties (name
,age
) and methods (sayHello
) fromPerson.prototype
.Also, Read: What is event delegation in JavaScript, and why is it useful?